package com.eastfair.notice.controller;

import com.eastfair.notice.dto.NoticePageQuery;
import com.eastfair.notice.entity.Notice;
import com.eastfair.notice.service.NoticeService;
import com.eastfair.notice.dto.NoticeSaveDTO;
import com.eastfair.notice.dto.NoticeUpdateDTO;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.eastfair.core.exception.code.ExceptionCode.PARAM_EX;


/**
 * <p>
 * 前端控制器
 * 站内信
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/notice")
@Api(value = "Notice", tags = "站内信")
public class NoticeController extends SuperController<NoticeService, Long, Notice, NoticePageQuery, NoticeSaveDTO, NoticeUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<Notice> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Notice> noticeList = list.stream().map((map) -> {
            Notice notice = Notice.builder().build();
            //TODO 请在这里完成转换
            return notice;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(noticeList));
    }

    /**
     * 站内信标记为已读
     * @param notice id
     * @return true/false
     */
    @PostMapping("/read")
    @ApiOperation(value = "站内信标记为已读",notes = "站内信标记为已读")
    public R<Boolean> read(@RequestBody Notice notice) {
        if (notice.getId() == null) {
            return fail(PARAM_EX);
        }
        return success(baseService.updateById(Notice.builder().id(notice.getId()).readStatus(Boolean.TRUE).build()));
    }

}
